/**
 * 用于读取本地数据和存储本地数据
 * index 设置用户名为 "", 登录时再合并数据
 */

let userName_preKey = "username_key";
let cartList_preKey = "cartsList_key";          // 购物车数据
let cartCount_preKey = "cartCount_key";         // 购物车商品数量
let goodsList_preKey = "goodsList_key";         // 商品数据
let goodsDetails_preKey = "goodsDetails_key";   // 商品详情总数据

class ProfileMgrClass{
    constructor(){
        this.init.call(this);
    }
    init(){
        this.m_userName = "";
        this.m_userName_dirt = false;

        this.m_cart_list = [];
        this.m_cartList_dirt = false;

        this.m_cart_count = 0;
        this.m_cartCount_dirt = false;

        this.m_goods_list = [];
        this.m_goodsList_dirt = false;

        this.m_goodsDetails = [];
        this.m_goodsDetails_dirt = false;

        this.m_load = false;
    }
    load(){
        // 获取本地用户名
        let mid = this.m_userName = localStorage.getItem(userName_preKey);
        // 获取本地购物车数据
        this.m_cart_list = JSON.parse(localStorage.getItem(cartList_preKey + mid) || "[]");
        // 获取购物车商品数量
        this.m_cart_count = parseInt(localStorage.getItem(cartCount_preKey + mid)) || 0;
        // 获取本地商品数据
        this.m_goods_list = JSON.parse(localStorage.getItem(goodsList_preKey) || "[]");
        // 获取商品详情总数据
        this.m_goodsDetails = JSON.parse(localStorage.getItem(goodsDetails_preKey) || "[]");

        this.m_load = true;
    }
    // 用于其他页面不知道是否读取过时
    repeatLoad(){
        if(this.m_load) return false;
        this.load();
    }
    save(){
        let mid = this.m_userName;
        // 本地存储用户名
        if(this.m_userName_dirt){
            localStorage.setItem(userName_preKey, mid);
            this.m_userName_dirt = false;
        }
        // 本地存储用户数据
        if(this.m_cartList_dirt){
            localStorage.setItem(cartList_preKey + mid, JSON.stringify(this.m_cart_list));
            this.m_cartList_dirt = false;
        }
        // 存储购物车商品数量
        if(this.m_cartCount_dirt){
            localStorage.setItem(cartCount_preKey + mid, this.m_cart_count);
            this.m_cartCount_dirt = false;
        }
        // 本地商品数据存储
        if(this.m_goodsList_dirt){
            localStorage.setItem(goodsList_preKey, JSON.stringify(this.m_goods_list));
            this.m_goodsList_dirt = false;
        }
        // 本地商品详情数据存储
        if(this.m_goodsDetails_dirt){
            localStorage.setItem(goodsDetails_preKey, JSON.stringify(this.m_goodsDetails));
            this.m_goodsDetails_dirt = false;
        }
    }
    // 合并没登录时的数据数据
    merge(){
        // 合并购物车数据
        let cartList = JSON.parse(localStorage.getItem(cartList_preKey) || "[]");
        let cartArr = [];
        for(let i = 0,item; item = cartList[i++];){
            let find = false;
            for(let j = 0,temp; temp= this.m_cart_list[j++];){
                if(item.goods_id == temp.goods_id){
                    temp.count += item.count;
                    find = true;
                    break;
                }
            }
            if(!find){
                cartArr.push(item);
            }
        }
        this.m_cart_list = this.m_cart_list.concat(cartArr);
        this.m_cartList_dirt = true;
        localStorage.setItem(cartList_preKey, "[]");
        
        // 合并购物车商品数量
        this.m_cart_count += (parseInt(localStorage.getItem(cartCount_preKey)) || 0);
        this.m_cartCount_dirt = true;
        localStorage.setItem(cartCount_preKey, 0);

        this.save();
    }
    // 处理用户名
    getUserName(){
        return this.m_userName;
    }
    setUserName(value){
        this.m_userName = value;
        this.m_userName_dirt = true;
        this.save();
    }
    // 处理购物车数据
    getCartList(){
        return this.m_cart_list;
    }
    setCartList(list){
        this.m_cart_list = list || [];
        this.m_cartList_dirt = true;
        this.save();
    }
    addCartList(value){
        this.m_cart_list.push(value);
        this.m_cartList_dirt = true;
        this.save();
    }
    // 通过商品id增加购物车数据
    addCartListById(goodsId, value=1){
        let find = this.m_cart_list.some(item=>{
            if(item.goods_id == goodsId){
                item.count += value;
                return true;
            }
        });
        if(!find){
            let item = this.getGoodsById(goodsId);
            item.count = value;
            this.m_cart_list.push(item);
        }
        this.m_cartList_dirt = true;
        this.save();
        
        // 增加购物车数量
        this.addCartCount(value);
    }
    // 指定购物车商品数量
    changeGoodsCountById(goodsId, value=1){
        let oriCnt = 0;
        let find = this.m_cart_list.some((item,index)=>{
            if(item.goods_id == goodsId){
                oriCnt = item.count;
                // value值为0时
                if(value <= 0){
                    let sel = confirm("确定删除此商品吗？");
                    if(!sel){
                        value = item.count;
                        return true;
                    }
                    else{
                        this.m_cart_list.splice(index,1);
                        return true;
                    }
                }
                item.count = value;
                return true;
            }
        });
        if(!find){
            let item = this.getGoodsById(goodsId);
            item.count = value;
            this.m_cart_list.push(item);
        }
        this.m_cartList_dirt = true;
        this.save();
        
        // 增加购物车数量
        this.addCartCount(value - oriCnt);
    }
    /**
     * 通过商品id移除购物车数据  
     * @param {*} goodsId   商品id
     * @param {*} flag      flag 为true将商品id为goodsId的全部移除
     */
    removeCartListById(goodsId,flag=false,hasTip){
        this.m_cart_list.some((item,index)=>{
            if(item.goods_id == goodsId){
                if(flag){
                    // 减少购物车数量
                    this.addCartCount(-item.count);
                    this.m_cart_list.splice(index,1);
                }
                else{
                    if(item.count == 1 && hasTip){
                        let sel = confirm("确定删除此商品吗？");
                        if(!sel) return true;
                    }
                    item.count--;
                    if(item.count <= 0)
                        this.m_cart_list.splice(index,1);
                    // 减少购物车数量
                    this.addCartCount(-1);
                }
                return true;
            }
        });
        this.m_cartList_dirt = true;
        this.save();
    }
    // 通过id找到商品
    getGoodsById(goodsId){
        let info = null;
        this.m_goods_list.some(item=>{
            if(item.goods_id == goodsId){
                info = item;
                return true;
            }
        });
        return info;
    }
    // 处理商品数据
    getGoodsList(){
        return this.m_goods_list;
    }
    setGoodsList(list){
        this.m_goods_list = list || this.m_goods_list;
        this.m_goodsList_dirt = true;
        this.save();
    }
    // 处理购物车商品数量
    getCartCount(){
        return this.m_cart_count;
    }
    setCartCount(value){
        value = parseInt(value) || 0;
        this.m_cart_count = value;
        this.m_cartCount_dirt = true;
        this.save();
    }
    addCartCount(value){
        value = parseInt(value) || 0;
        this.m_cart_count += value;
        this.m_cartCount_dirt = true;
        this.save();
    }
    // 商品详情
    getGoodsDetails(){
        return this.m_goodsDetails;
    }
    setGoodsDetails(list){
        this.m_goodsDetails = list || [];
        this.m_goodsDetails_dirt = true;
        this.save();
    }
    getDetailById(goodsId){
        let info = null;
        this.m_goodsDetails.some(item=>{
            if(item.goods_id == goodsId){
                info = item;
                return true;
            }
        });
        return info;
    }
}
if (window.ProfileMgr === undefined) {
    window.ProfileMgr = new ProfileMgrClass();
}

module.exports = ProfileMgr;